package com.sort;

public class QuickSortByJava {

    private void quickSort(int[] arr, int left, int right) {
        if (left >= right) {
            return;
        }
        int l = left;
        int r = right;

        int p = arr[l];

        while (l < r) {
            while (p <= arr[r] && l < r) {
                r--;
            }

            while (p >= arr[l] && l < r) {
                l++;
            }

            swap(arr, l, r);
        }

        swap(arr, left, l);

        quickSort(arr, left, l);
        quickSort(arr, l + 1, right);
    }

    void swap(int[] arr, int i, int j) {
        int t = arr[i];
        arr[i] = arr[j];
        arr[j] = t;
    }

    public static void main(String[] args) {
//        int[] arr = new int[]{4, 3};
        int[] arr = new int[]{4, 3, 12, 4, 0, 18, 9};
//        int[] arr = new int[]{4, 3, 12};

        QuickSortByJava quickSortByJava = new QuickSortByJava();
        quickSortByJava.quickSort(arr, 0, arr.length - 1);

        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }

    }

}
